package com.etc.controller;

import java.util.List;

import org.aspectj.lang.annotation.RequiredTypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleIfStatement.ElseIf;

import com.etc.entity.User;
import com.etc.mapper.UserMapper;

import tk.mybatis.mapper.entity.Example;

@Controller
public class UserController {

	@Autowired
	private UserMapper  usermapper;
	
	
	/**
	 * 查看用户名是否存在
	 */
	@RequestMapping(value="checkUserName",produces="application/json;charset=utf-8")
	@ResponseBody
	public String checkName(String username) 
	{
		//根据用户名去查用户是否存在
		System.out.println(username);	
		User us = usermapper.chackname(username);
		if(us!=null) 
		{
			return "{\"msg\":\"false\"}";
		}
		return "{\"msg\":\"true\"}";
	}
	/**
	 * 查看用户邮箱是否存在
	 */
	@RequestMapping(value="checkUserEmail",produces="application/json;charset=utf-8")
	@ResponseBody
	public String checkEmail(String userEmail) 
	{
		//根据用户名去查用户是否存在
		System.out.println(userEmail);	
		User us = usermapper.chackEmai(userEmail);
		if(us!=null) 
		{
			return "{\"msg\":\"false\"}";
		}
		return "{\"msg\":\"true\"}";
	}
	/**
	 * 查看用户手机是否存在
	 */
	@RequestMapping(value="checkUserTel",produces="application/json;charset=utf-8")
	@ResponseBody
	public String checkUserTel(String userTel) 
	{
		//根据用户名去查用户是否存在
		System.out.println(userTel);	
		User us = usermapper.chackuserTel(userTel);
		if(us!=null) 
		{
			return "{\"msg\":\"false\"}";
		}
		return "{\"msg\":\"true\"}";
	}

	/**
	 * 用户登录方法
	 */
  @PostMapping(value="loginUser",produces="application/json;charset-utf-8")
  public String login(User us,Model model) 
  {
	  User login = usermapper.login(us.getuName(), us.getuPwd());
      System.out.println(login); 
	  if(login!=null) 
        {
        	model.addAttribute("user", login);
        	return "forward:Category.jsp";
        }
        else 
        {
        	model.addAttribute("msg", "用户账号密码有误，请重新登录");
        	return "Category";
        }
  }
  /**
   * 用户进行注册
   */
   
  @PostMapping(value="registUser")
  public String regist(User us,ModelMap model) 
  {
	
	  int insert = usermapper.insert(us);
	  System.out.println(us.getuEmail());
	  if(insert>0) 
	  {
		  model.addAttribute("msg", "注册成功，请重新登录");		  
		  return "forward:Login.jsp";
	  }
	  else
	  {
		  model.addAttribute("msg", "注册失败，请重新注册");
		  return "Regist";
	  }
  }

}
